package com.itheima.ydd.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.entity.Menu;
import com.itheima.ydd.entity.Role;
import com.itheima.ydd.mapper.MenuMapper;
import com.itheima.ydd.mapper.RoleMapper;
import com.itheima.ydd.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author yan.zhou
 * @since 2022-02-22
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<String> roleNameList(String role_id) {
        String[] strings = role_id.split(",");
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper.in(Role::getId, strings);
        List<Role> roles = this.list(roleLambdaQueryWrapper);
        List<String> roleNameList = CollUtil.getFieldValues(roles, "name", String.class);
        return roleNameList;
    }

    @Override
    public Role rowInfo(String id) {
        Role role = roleMapper.selectById(id);
        if (role == null) {
            throw new BusinessException("角色不存在");
        }
        Integer[] menuIds = roleMapper.getMenuIds(id);
        Arrays.sort(menuIds);
        List<Menu> allData = getAllData(0, menuIds);
        role.setAuthList(allData);
        return role;
    }

    /**
     * 角色添加
     *
     * @param role
     */
    @Override
    @Transactional
    public void add(Role role) {
        if (roleMapper.selectOne(new LambdaQueryWrapper<Role>().eq(Role::getName, role.getName())) != null) {
            throw new BusinessException("角色名称重复");
        }
        roleMapper.insert(role);
        String id = role.getId();
        Integer[] menuids = role.getMenuids();
        roleMapper.addmenuid(menuids, id);
    }

    /**
     * 角色修改
     *
     * @param role
     */
    @Override
    @Transactional
    public void updateAll(Role role) {
        roleMapper.updateById(role);
        String id = role.getId();
        roleMapper.deleteMenid(id);
        Integer[] menuids = role.getMenuids();
        roleMapper.addmenuid(menuids, id);
    }

    private List<Menu> getAllData(Integer pip, Integer[] ids) {
        List<Menu> list = menuMapper.selectList(new LambdaQueryWrapper<Menu>().eq(Menu::getPid, pip));
        list.removeIf(menu -> Arrays.binarySearch(ids, menu.getId()) < 0);
        if (list.isEmpty()) {
            return list;
        }
        for (Menu menu : list) {
            Integer id = menu.getId();
            if (Arrays.binarySearch(ids, id) >= 0) {
                List<Menu> allData = getAllData(id, ids);
                menu.setChildren(allData);
            }
        }
        return list;
    }
}
